/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package lyra;

import java.util.List;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import javax.swing.table.AbstractTableModel;
import lyra.pojos.*;

/**
 *
 * @author Koustas, Marmaridis, Mariolou
 */
public class FArtist extends javax.swing.JInternalFrame {

    Artist artist;
    Artist OriginalArtist;
    List<?> curArtistMusicGenre;
    boolean bChanges = false;
    boolean bFormLoaded = false;
    boolean bFormClosing = false;
    
    /**
     * Creates new form frmArtist
     */
   private FListArtist parentForm;

   SimpleDateFormat sDF = new SimpleDateFormat("dd/MM/yyyy");
   
   public class ArtistGenreTableModel extends AbstractTableModel 
   {
   
       private List<?> ArtistGenreList;
           
       private final String[] columnNames = new String[] {
                "MusicGenreId", "Τύπος Μουσικής", "Επιλογή"
        };
        private final Class[] columnClass = new Class[] {
            Long.class, String.class, Boolean.class
        };
 
        public ArtistGenreTableModel(List<?> ArtistGenreList)
        {
           this.ArtistGenreList = ArtistGenreList;
        }
     
        @Override
        public String getColumnName(int column)
        {
            return columnNames[column];
        }
 
        @Override
        public Class<?> getColumnClass(int columnIndex)
        {
            return columnClass[columnIndex];
        }

        @Override
        public int getColumnCount()
        {
            return columnNames.length;
        }

        @Override
        public int getRowCount()
        {
            return ArtistGenreList.size();
        }
        
        @Override
        public boolean isCellEditable(int row, int col) {
            return true;
        }
        
        @Override
        public void setValueAt(Object obj, int rowIndex, int columnIndex) {
            try {
                Object[] row = (Object[]) this.ArtistGenreList.get(rowIndex);
                switch (columnIndex) {
                    case 2: {
                        if (bFormLoaded) setChanges(true);
                        if ((boolean)obj==true) {
                            row[3] = 1;
                        } else {
                            row[3] = 0;
                        }
                    }
                    default: {
                        
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace(System.out);
            }
        }
                
        @Override
        public Object getValueAt(int rowIndex, int columnIndex)
        {
            try {
                Object[] row = (Object[]) this.ArtistGenreList.get(rowIndex);
                switch (columnIndex) {
                    case 0: {
                        return row[0];
                    }
                    case 1: {
                        return row[1];
                    }
                    case 2: {
                        if((int)row[3]==0) {
                            return false;
                        } else {
                            return true;
                        }
                    }
                    default: {
                        return null;
                    }
                }
            } catch (Exception ex) {
                    ex.printStackTrace(System.out);
                    return null;
            }

        }
   }
    
    
   final void fillArtistGenre(long artistID) {
       
        curArtistMusicGenre = new ArrayList();
        
        if (artistID == -1) {
            Query qArtistGenre =  LYRA.e.createNativeQuery("SELECT mg.*, -1 AS ArtistID, 0 AS chkValue FROM ADMIN1.MusicGenre mg");
            curArtistMusicGenre = (List<?>)qArtistGenre.getResultList();
        } else {
            Query qArtistGenre =  LYRA.e.createNativeQuery("SELECT ag.* FROM ADMIN1.Vwartistgenre ag WHERE ag.ARTISTID = " + artistID);
            curArtistMusicGenre =  (List<?>)qArtistGenre.getResultList();
        }
        
        ArtistGenreTableModel model = new ArtistGenreTableModel(curArtistMusicGenre);
        
        jTableArtistGenre.setModel(model);

    }
   
    public FArtist(FListArtist parent) {

        initComponents();
        
        this.parentForm = parent;
        
        this.setTitle("Εισαγωγή στοιχείων καλλιτέχνη");
        Insertion();
        dtDateBirth.setFormats("dd/MM/yyyy");
        setLockCtrls(false);
        fillArtistGenre(-1);
        bFormLoaded = true;
        LYRA.setFrameIcon(this,"microphone20.png");
        parentForm.EnableDisableForm(false);
    }
    
    public FArtist(FListArtist parent, Artist art ) {

        OriginalArtist = art;
        initComponents();
        artist = OriginalArtist;
        if (art.getSex() != null) {
            if (art.getSex().equals('M')) {
                cmbSex.setSelectedIndex(1);
            } else if (art.getSex().equals('F')) {
                cmbSex.setSelectedIndex(2);
            }        
        }
        this.parentForm = parent;
        
        this.setTitle("Μεταβολή στοιχείων καλλιτέχνη");
        dtDateBirth.setFormats("dd/MM/yyyy");
        setLockCtrls(false);
        fillArtistGenre(art.getArtistid());
        bFormLoaded = true;
        LYRA.setFrameIcon(this,"microphone20.png");
        parentForm.EnableDisableForm(false);
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        LYRAPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("LYRAPU").createEntityManager();
        lyra.pojos.Artist artist = OriginalArtist;
        tbarManage = new javax.swing.JToolBar();
        tbbModify = new javax.swing.JButton();
        tbbInsert = new javax.swing.JButton();
        tbbDelete = new javax.swing.JButton();
        tbbExit = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        cmbSex = new javax.swing.JComboBox();
        txtLastname = new javax.swing.JTextField();
        txtFirstname = new javax.swing.JTextField();
        txtArtistname = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtPlaceBirth = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        dtDateBirth = new org.jdesktop.swingx.JXDatePicker();
        jLabel7 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTableArtistGenre = new javax.swing.JTable();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Καλλιτέχνες");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosing(evt);
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });

        tbarManage.setFloatable(false);
        tbarManage.setRollover(true);

        tbbModify.setText("Αποθήκευση");
        tbbModify.setFocusable(false);
        tbbModify.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        tbbModify.setPreferredSize(new java.awt.Dimension(50, 50));
        tbbModify.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        tbbModify.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tbbModifyActionPerformed(evt);
            }
        });
        tbarManage.add(tbbModify);

        tbbInsert.setText("Εισαγωγή");
        tbbInsert.setFocusable(false);
        tbbInsert.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        tbbInsert.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        tbbInsert.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tbbInsertActionPerformed(evt);
            }
        });
        tbarManage.add(tbbInsert);

        tbbDelete.setText("Διαγραφή");
        tbbDelete.setFocusable(false);
        tbbDelete.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        tbbDelete.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        tbbDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tbbDeleteActionPerformed(evt);
            }
        });
        tbarManage.add(tbbDelete);

        tbbExit.setText("Έξοδος");
        tbbExit.setFocusable(false);
        tbbExit.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        tbbExit.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        tbbExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tbbExitActionPerformed(evt);
            }
        });
        tbarManage.add(tbbExit);

        jPanel1.setBackground(new java.awt.Color(204, 255, 255));

        jLabel1.setText("Επώνυμο:");

        jLabel3.setText("Καλλιτεχν. όνομα:");

        cmbSex.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Επιλέξτε φύλο", "Άνδρας", "Γυναίκα" }));

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, artist, org.jdesktop.beansbinding.ELProperty.create("${selectSex}"), cmbSex, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);

        cmbSex.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbSexActionPerformed(evt);
            }
        });

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, artist, org.jdesktop.beansbinding.ELProperty.create("${lastname}"), txtLastname, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        txtLastname.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtLastnameKeyTyped(evt);
            }
        });

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, artist, org.jdesktop.beansbinding.ELProperty.create("${firstname}"), txtFirstname, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        txtFirstname.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtFirstnameKeyTyped(evt);
            }
        });

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, artist, org.jdesktop.beansbinding.ELProperty.create("${artisticname}"), txtArtistname, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        txtArtistname.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtArtistnameKeyTyped(evt);
            }
        });

        jLabel4.setText("Φύλο");

        jLabel5.setText("Ημερομηνία γέννησης:");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, artist, org.jdesktop.beansbinding.ELProperty.create("${birthplace}"), txtPlaceBirth, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        txtPlaceBirth.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtPlaceBirthKeyTyped(evt);
            }
        });

        jLabel2.setText("Όνομα:");

        jLabel6.setText("Τόπος γέννησης:");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, artist, org.jdesktop.beansbinding.ELProperty.create("${birthday}"), dtDateBirth, org.jdesktop.beansbinding.BeanProperty.create("date"));
        bindingGroup.addBinding(binding);

        dtDateBirth.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                dtDateBirthKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtArtistname)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtPlaceBirth, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtLastname, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtFirstname, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(cmbSex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(dtDateBirth, javax.swing.GroupLayout.PREFERRED_SIZE, 107, Short.MAX_VALUE)))
                        .addContainerGap())))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtLastname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtFirstname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtArtistname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbSex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(dtDateBirth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtPlaceBirth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel7.setBackground(new java.awt.Color(51, 255, 51));
        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel7.setText("Είδος μουσικής που υπηρετεί ο καλλιτέχνης");

        jTableArtistGenre.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane3.setViewportView(jTableArtistGenre);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tbarManage, javax.swing.GroupLayout.PREFERRED_SIZE, 354, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tbarManage, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void StartEdit()
    {
        setLockCtrls(false);
        //setChanges(false);
    }
            
    private void FinisEdit()
    {
        setLockCtrls(true);

        tbbModify.setEnabled(true);
        //tbbModify.setIcon("imgUpdate.png");
    }
    
    private void setLockCtrls(boolean lock)
    {
        if (lock) 
        {
            tbbModify.setText("Μεταβολή");
            tbbExit.setText("Έξοδος");
        }
        else
        {
            tbbModify.setText("Αποθήκευση");
            tbbExit.setText("Ακύρωση");
        }
        
        tbbModify.setEnabled(bChanges);
        tbbInsert.setEnabled(lock);
        tbbDelete.setEnabled(lock);
    
        txtFirstname.setEditable(!lock);
        txtLastname.setEditable(!lock);
        txtArtistname.setEditable(!lock);
        cmbSex.setEnabled(!lock);
        dtDateBirth.setEditable(!lock);
        txtPlaceBirth.setEditable(!lock);
        jTableArtistGenre.setEnabled(!lock);
    }
    
    private boolean isDataOK()
    {
        //Έλεγχος ύπαρξης Επωνύμου & Ονόματος
        if ( (txtLastname.getText().trim()).length() == 0 )
        {
            LYRA.MsgBox("Πρέπει να δοθεί επώνυμο υποχρεωτικά", "ΠΡΟΣΟΧΗ");
            txtLastname.setFocusable(true);
            return false;
        }
        if ( (txtFirstname.getText().trim()).length() == 0 )
        {
            LYRA.MsgBox("Πρέπει να δοθεί όνομα υποχρεωτικά", "ΠΡΟΣΟΧΗ");
            txtFirstname.setFocusable(true);
            return false;
        }
        //έλεγχος combo
        if (cmbSex.getSelectedIndex()<1) 
        {
            LYRA.MsgBox("Δεν έχει επιλεγεί το φύλο του καλλιτέχνη", "ΠΡΟΣΟΧΗ");
            cmbSex.setFocusable(true);
            return false;
        }
        //αν φτάσει ως εδώ τα δεδομένα είναι ΟΚ
        return true;
    }

    private void txtLastnameKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtLastnameKeyTyped
        if (bFormLoaded) setChanges(true);
    }//GEN-LAST:event_txtLastnameKeyTyped

    private void txtFirstnameKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtFirstnameKeyTyped
        if (bFormLoaded) setChanges(true);
    }//GEN-LAST:event_txtFirstnameKeyTyped
            
    private void txtArtistnameKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtArtistnameKeyTyped
        if (bFormLoaded) setChanges(true);
    }//GEN-LAST:event_txtArtistnameKeyTyped

    private void tbbModifyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tbbModifyActionPerformed
        UpdateArtist();
    }//GEN-LAST:event_tbbModifyActionPerformed

    private void tbbInsertActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tbbInsertActionPerformed
        Insertion();
    }//GEN-LAST:event_tbbInsertActionPerformed

    private void tbbDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tbbDeleteActionPerformed
        DeleteArtist();
    }//GEN-LAST:event_tbbDeleteActionPerformed

    private void  DeleteArtist() {
        //Διαγραφή καλλιτέχνη
        if ( LYRA.yesno("Θέλετε σίγουρα να διαγραφεί ο καλλιτέχνης " +  artist.getLastname().trim()+ " " + artist.getFirstname() ) == 0 )
        {
            try {
                EntityTransaction et = null;
                et = LYRA.e.getTransaction();
                et.begin();
                
                Query queryDelete =  LYRA.e.createNativeQuery("DELETE FROM ADMIN1.Artist a WHERE a.ARTISTID = " + artist.getArtistid().toString());
                queryDelete.executeUpdate();

                et.commit();
                parentForm.RefreshArtistTable();
                //Ενεργοποίηση της προηγούμενης φόρμας
                parentForm.EnableDisableForm(true);
                //Κλείσιμο φόρμας
                dispose();
            }
            catch (Exception e) {
                LYRA.MsgBox(e.getMessage(),"ΠΡΟΣΟΧΗ");
            }
            
        }
    }
    
    private boolean CloseForm() {
        
        if (!tbbExit.isEnabled()) return false;
        
        if (tbbExit.getText()=="Έξοδος")
        {
            LYRA.e.clear();
            parentForm.RefreshArtistTable();
            parentForm.setEnabled(true);
            return true;
        }
        else
        {
            if (bChanges)
            {
                if ( LYRA.yesno("Είστε σίγουροι ότι θέλετε να χαθούν οι αλλαγές; ") == 0 )
                {
                    LYRA.e.clear();
                    parentForm.RefreshArtistTable();
                    return true;
                } else {
                    FinisEdit();
                }
             } else {
                return true;
            }
        }
        
        return false;
    }
            
    private void tbbExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tbbExitActionPerformed
        if (CloseForm()) {
            bFormClosing = true;
            parentForm.EnableDisableForm(true);
            dispose();
        }
    }//GEN-LAST:event_tbbExitActionPerformed

    private void txtPlaceBirthKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPlaceBirthKeyTyped
        if (bFormLoaded) setChanges(true);
    }//GEN-LAST:event_txtPlaceBirthKeyTyped

    private void dtDateBirthKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_dtDateBirthKeyTyped
        if (bFormLoaded) setChanges(true);
    }//GEN-LAST:event_dtDateBirthKeyTyped

    private void formInternalFrameClosing(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosing
        if (!bFormClosing) {
            if (CloseForm()) {
                bFormClosing = true;
                parentForm.EnableDisableForm(true);
                dispose();
            }
        } else {
            parentForm.EnableDisableForm(true);
            dispose();
        }
    }//GEN-LAST:event_formInternalFrameClosing

    private void cmbSexActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbSexActionPerformed
        if (bFormLoaded) setChanges(true);
    }//GEN-LAST:event_cmbSexActionPerformed

    private void UpdateArtist() {

        if (tbbModify.getText()=="Μεταβολή")
        {
            StartEdit();
        }
        else
        {
            if (isDataOK())
            {
                
                EntityTransaction et = null;
                
                
                try {
                    
                    Query mQueryMusicGenre = LYRA.e.createQuery("SELECT mg FROM Musicgenre mg");
                    Collection<Musicgenre> mg = (Collection<Musicgenre>) mQueryMusicGenre.getResultList();
                    
                    artist.setSex(null);
                    if (cmbSex.getSelectedIndex() == 1) {
                        artist.setSex('M');
                    } else if (cmbSex.getSelectedIndex() == 2) {
                        artist.setSex('F');
                    }

                    et = LYRA.e.getTransaction();
                    et.begin();
                    if (artist.getMusicgenreCollection() == null) {
                        Collection<Musicgenre> emptyCollection = new ArrayList<Musicgenre>();
                        artist.setMusicgenreCollection(emptyCollection);
                    } else {
                        artist.getMusicgenreCollection().clear();
                    }
                    Collection <Musicgenre> mArtistMusicGenreCollection = artist.getMusicgenreCollection();
                    
                    if (curArtistMusicGenre.isEmpty() == false) {
                        for (int curRow=0;curRow<curArtistMusicGenre.size();curRow++) {
                            Object[] row = (Object[]) curArtistMusicGenre.get(curRow);
                            if ((int)row[3]==1) {
                                 if (mg.isEmpty()) {
                                       // do nothing
                                 } else {
                                    // lambda expression works for java 1.8
                                    /*mg.stream().forEach((item) -> {
                                        if (item.getMusicgenreid() == (long)row[0]) {
                                            mArtistMusicGenreCollection.add(item);
                                        }
                                    });*/
                                     
                                     // since the other members work in java 1.7 so be it
                                     Iterator mg_iter = mg.iterator();
                                     while (mg_iter.hasNext()) {
                                         Musicgenre curmg = (Musicgenre) mg_iter.next();
                                         if (curmg.getMusicgenreid() == (long)row[0]) {
                                             mArtistMusicGenreCollection.add(curmg);
                                         }
                                     }
                                 }
                                
                                //mArtistMusicGenreCollection.add(new Musicgenre((long)row[0]));
                                /*
                                        Query queryInsert = LYRA.e.createNativeQuery("INSERT INTO ADMIN1.ArtistGenre (MusicGenreID, ArtistID) VALUES (" +
                                                                             row[0] + "," + artist.getArtistid() +")");
                                        queryInsert.executeUpdate();
                                */
                            }
                        }

                    }

                    artist.setMusicgenreCollection(mArtistMusicGenreCollection);
                    LYRA.e.persist(artist);

                    /*
                    if (artist.getArtistid() != null) {
                        Query queryDelete = LYRA.e.createNativeQuery("DELETE FROM ADMIN1.ArtistGenre WHERE ArtistID = " + artist.getArtistid());
                        queryDelete.executeUpdate();
                    }*/
                    

                    et.commit();
                    FinisEdit();
                    parentForm.RefreshArtistTable();
                    // Αφού αποθηκεύσαμε κλείνουμε και τη φόρμα
                    parentForm.EnableDisableForm(true);
                    this.dispose();
                } catch (Exception ex) {
                    LYRA.e.clear();
                    if (et.isActive()) et.rollback();
                    ex.printStackTrace(System.out);
                    LYRA.MsgBox(ex.getMessage(), "Πρόβλημα");
                }
                
            }
        }
    }
    
    private void setChanges( boolean modified )
    {
        if (modified)
        {
            tbbModify.setEnabled(true);
            tbbExit.setEnabled(true);            
        }
        //Ένδειξη ύπαρξης/μη ύπαρξης αλλαγών
        bChanges= modified;        
    }
    
    public final void Insertion() {

        //Δημιουργία νέου αντικειμένου artist της κλάσης Artist
        artist = new Artist();
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();
        
        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, artist, org.jdesktop.beansbinding.ELProperty.create("${selectSex}"), cmbSex, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, artist, org.jdesktop.beansbinding.ELProperty.create("${lastname}"), txtLastname, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, artist, org.jdesktop.beansbinding.ELProperty.create("${firstname}"), txtFirstname, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, artist, org.jdesktop.beansbinding.ELProperty.create("${artisticname}"), txtArtistname, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, artist, org.jdesktop.beansbinding.ELProperty.create("${birthplace}"), txtPlaceBirth, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, artist, org.jdesktop.beansbinding.ELProperty.create("${birthday}"), dtDateBirth, org.jdesktop.beansbinding.BeanProperty.create("date"));
        bindingGroup.addBinding(binding);
        
        bindingGroup.bind();
        
        setChanges(false);
        
        /*if (artist.getArtistid() == null) {
            List <Object> record = LYRA.e.createNativeQuery( "SELECT MAX(Artistid) FROM ARTIST ").getResultList();                
            for( Object rec: record) artist.setArtistid((Long) rec+1);
        }*/
        
        StartEdit();
        txtLastname.requestFocus();

    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.persistence.EntityManager LYRAPUEntityManager;
    private javax.swing.JComboBox cmbSex;
    private org.jdesktop.swingx.JXDatePicker dtDateBirth;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable jTableArtistGenre;
    private javax.swing.JToolBar tbarManage;
    private javax.swing.JButton tbbDelete;
    private javax.swing.JButton tbbExit;
    private javax.swing.JButton tbbInsert;
    private javax.swing.JButton tbbModify;
    private javax.swing.JTextField txtArtistname;
    private javax.swing.JTextField txtFirstname;
    private javax.swing.JTextField txtLastname;
    private javax.swing.JTextField txtPlaceBirth;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}

